﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Supermarket.Bll;
using System.Web.Security;

namespace Supermarket.Web.CashierPages
{
    public partial class AddToCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["productId"] != null && Request.Params["quantity"] != null)
            {
                if (Session["Supermarket_CartID"] == null)
                {
                    Session["Supermarket_CartID"] = Membership.GetUser().ProviderUserKey.ToString();
                }
                string cartId = Session["Supermarket_CartID"].ToString();
                int productId = int.Parse(Request.Params["productId"]);
                string stringQuantity = Request.Params["quantity"];
                double quantity;
                try
                {                    
                    quantity = double.Parse(stringQuantity);
                }
                catch (FormatException)
                {
                    stringQuantity = stringQuantity.Replace(".", ",");
                    quantity = double.Parse(stringQuantity);
                }

                if (Request.Params["productPrice"] != null)
                {
                    string stringPrice = Request.Params["productPrice"];
                    decimal productPrice;
                    try
                    {
                        productPrice = decimal.Parse(stringPrice);
                    }
                    catch (FormatException)
                    {
                        stringPrice = stringPrice.Replace(".", ",");
                        productPrice = decimal.Parse(stringPrice);
                    }
                    ShoppingCartManager.AddItemForSupply(cartId, productId, productPrice, quantity);

                    Response.Redirect("~/CashierPages/SupplyProducts.aspx?show=1");
                }
                ShoppingCartManager.AddItemForSale(cartId, productId, quantity);
                Response.Redirect("~/CashierPages/Sales.aspx?show=1");
            }

            Response.Redirect("~/Default.aspx");
        }
    }
}